package com.beilang.hotline.person.page;

import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.core.page.BasePage;
import com.beilang.base.exception.CheckException;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.CodeConstant;
import com.beilang.base.util.DateUtil;
import com.beilang.base.util.StringUtil;
import com.beilang.base.property.AppDefaultProperty;
import com.beilang.hotline.person.manager.PersonMgr;
import com.beilang.hotline.person.model.PersonModel;
import com.beilang.hotline.person.to.SearchTO;
import com.beilang.hotline.user.model.UserModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("unchecked")
public class PersonPage extends BasePage {
    private PersonMgr personMgr;
    private CodeLookupMgr codeLookupMgr;

    public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
        this.codeLookupMgr = codeLookupMgr;
    }

    public void setPersonMgr(PersonMgr personMgr) {
        this.personMgr = personMgr;
    }

    public List getSearchAreaList() {
        List list = new ArrayList();
        list.add(this.getEmptyCodeModel());
        list.addAll(codeLookupMgr.getCodeList(CodeConstant.CODE_CUSTOM_AREA_LIST));
        return list;
    }

    public List getSearchCompanyList() {
        List list = new ArrayList();
        list.add(getEmptyCodeModel());
        list.addAll(getAddCompanyList());
        return list;
    }

    public List getSearchCompanyListWithFilter(String customAreaId) {
        List list = new ArrayList();
        list.add(getEmptyCodeModel());
        if(StringUtil.isNullOrBlank(customAreaId)){
            list.addAll(getAddCompanyList());
        }else{
            List l=new ArrayList();
            l.add(customAreaId);
            list.addAll(codeLookupMgr.getCodeListWithFilter(CodeConstant.COMPANY_NAME_FILTER,CodeConstant.CODE_FILTER,l));
        }
        return list;
    }

    public List getAddCompanyList() {
        return codeLookupMgr.getCodeList(CodeConstant.COMPANY_NAME);
    }

    public List getUpdateCompanyList() {
        return getAddCompanyList();
    }

    public List getSearchSysList() {
        List list = new ArrayList();
        list.add(getEmptyCodeModel());
        list.addAll(getAddSysList());
        return list;
    }

    public List getAddSysList() {
        return codeLookupMgr.getCodeListWithFilter(CodeConstant.SYS_NAME, CodeConstant.CODE_FILTER, new ArrayList());
    }

    public List getViewDetailSysList() {
        return getAddSysList();
    }

    public List getUpdateSysList() {
         return getAddSysList();
    }    

    
    public List getAddSexList() {
        List list = new ArrayList();
        list.add(getEmptyCodeModel());
        list.addAll(codeLookupMgr.getCodeList(CodeConstant.CODE_SEX_LIST));
        return list;
    }

    public List getUpdateSexList() {
        return this.getAddSexList();
    }    

    public PaginationHelper getSearchList(PaginationHelper helper, SearchTO searchTO) {
        helper = personMgr.getPersonList(helper, searchTO);
        return helper;
    }

    public PaginationHelper getSearchListWithoutC(PaginationHelper helper, SearchTO searchTO) {
        helper = personMgr.getPersonList(helper, searchTO);
        if(helper!=null&&helper.getList()!=null&&helper.getList().size()>0){
            List list=helper.getList();
            List l=new ArrayList();
            for(int i=0;i<list.size();i++){
                PersonModel p=(PersonModel)list.get(i);
                p.setPersonName(StringUtil.splitString(p.getPersonName()));
                l.add(p);
            }
            helper.setList(l);
        }
        return helper;
    }

    public PaginationHelper deletePersonList(PaginationHelper helper, SearchTO searchTO, List deleteList) {
        for (Object obj : deleteList) {
            PersonModel personModel = (PersonModel) obj;
            personMgr.deletePerson(personModel.getPersonId());
        }
        return this.getSearchList(helper, searchTO);
    }

    public PersonModel addPerson(PersonModel personModel) throws CheckException {
        UserModel user = this.getLoginUser();
        personModel.setCreateId(user.getUserId());
        personModel.setUpdateId(user.getUserId());
        Date date = DateUtil.getDate();
        personModel.setCreateTime(date);
        personModel.setUpdateTime(date);
        List list=personMgr.getPersonList(personModel.getPersonName(),personModel.getTelNo(),personModel.getHandPhone());
        if(list!=null&& list.size()>0){
            throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_RECORD_ALREADY_EXIST));
        }
        personModel = personMgr.createPerson(personModel);
        return personModel;
    }

    public PersonModel getViewPersonNotC(Long personId) {
        PersonModel personModel=  personMgr.getPerson(personId);
        if (personModel!=null)
        {
            personModel.setPersonName(StringUtil.splitString(personModel.getPersonName()));
        }
        return personModel;
    }
    public PersonModel getViewPerson(Long personId) {
        return personMgr.getPerson(personId);
    }

    public PersonModel getUpdatePerson(Long personId) {
        return getViewPerson(personId);
    }

    public PersonModel updatePerson(PersonModel personModel) throws CheckException{
        List list=personMgr.getPersonList(personModel.getPersonName(),personModel.getTelNo(),personModel.getHandPhone());
        if(list!=null&& list.size()==1){
            PersonModel p=(PersonModel)list.get(0);
            if(p.getPersonId().longValue()!=personModel.getPersonId().longValue()){
                throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_RECORD_ALREADY_EXIST));    
            }
        }
        if(list!=null&& list.size()>1){
                throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_RECORD_ALREADY_EXIST));
        }
        UserModel user = this.getLoginUser();
        personModel.setUpdateId(user.getUserId());
        personModel.setUpdateTime(DateUtil.getDate());
        personModel = personMgr.savePerson(personModel);
        return personModel;
    }

}
